﻿using CaltechSoft.Service.DBCommand;
using CaltechSoft.Service.EntityFW.Context;
using CaltechSoft.Service.EntityFW.Entity;
using CaltechSoft.Service.EntityFW.Repository;
using CaltechSoft.Service.Infrastructure.Utility;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaltechSoft.Service.CommandService.WorkOrder
{
    public class WorkOrderCommandService : IWorkOderCommandService
    {
        private readonly IUnitOfWork _unitOfWork;
        public WorkOrderCommandService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;

        }


        public int CreateWorkOrder(EntityFW.Entity.WorkOrder workOrder)
        {
            Guard.ArgumentIsNotNull(workOrder, "workOrder can not null");

            try
            {
                using (var context = new CaltechContext())
                {
                    var numberOfWorkOrder = context.WorkOrders.Count(x => x.CreatedDate.HasValue && x.CreatedDate.Value.Year == DateTime.Now.Year && x.CreatedDate.Value.Month == DateTime.Now.Month && x.CreatedDate.Value.Day == DateTime.Now.Day);

                    workOrder.WorkOrderNo = "W" + DateTime.Now.ToString("MMddyy") + "-" + (numberOfWorkOrder + 1).ToString("000");

                    foreach (var item in workOrder.WorkOrderDetails)
                    {
                        context.WorkOrderDetails.Add(item);
                    }

                    context.WorkOrders.Add(workOrder);
                    context.SaveChanges();
                    return workOrder.WorkOrderId;
                }
            }
            catch (DbEntityValidationException e)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                    eve.Entry.Entity.GetType().Name,
                                                    eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                    ve.PropertyName,
                                                    ve.ErrorMessage));
                    }
                }
                throw new DbEntityValidationException(sb.ToString(), e);
            }
        }

        public void Delete(int workOrderId)
        {
            var WODetails = _unitOfWork.Repository<WorkOrderDetails>().Get(n => n.WorkOrderId == workOrderId);
            foreach (var item in WODetails)
            {
                _unitOfWork.Repository<WorkOrderDetails>().Delete(item.Id);
                _unitOfWork.Save();
            }
            _unitOfWork.Repository<EntityFW.Entity.WorkOrder>().Delete(workOrderId);
            _unitOfWork.Save();
        }

        public int UpdateWorkOrder(EntityFW.Entity.WorkOrder workOrder)
        {
            Guard.ArgumentIsNotNull(workOrder, "workOrder can not null");

            try
            {
                var certificateDetails = _unitOfWork.Repository<WorkOrderDetails>().Get(n => n.WorkOrderId == workOrder.WorkOrderId);

                foreach (var item in certificateDetails)
                {
                    _unitOfWork.Repository<WorkOrderDetails>().Delete(item.Id);
                    _unitOfWork.Save();
                }

                foreach (var item in workOrder.WorkOrderDetails)
                {
                    item.WorkOrderId = workOrder.WorkOrderId;
                    _unitOfWork.Repository<WorkOrderDetails>().Insert(item);
                    _unitOfWork.Save();
                }

                _unitOfWork.Repository<EntityFW.Entity.WorkOrder>().Update(workOrder);
                _unitOfWork.Save();
                return workOrder.WorkOrderId;

            }
            catch (DbEntityValidationException e)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                    eve.Entry.Entity.GetType().Name,
                                                    eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                    ve.PropertyName,
                                                    ve.ErrorMessage));
                    }
                }
                throw new DbEntityValidationException(sb.ToString(), e);
            }
        }
    }
}
